﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using theTVDB;
using theTVDB.Xml;

namespace WinSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TVDB tvdbObj;

        public MainWindow()
        {
            InitializeComponent();

            string apiKey = MasterSettings.Default["ApiKey"] as string;

            if (apiKey == string.Empty)
                throw new Exception("Please provide the ApiKey in the settings");

            this.tvdbObj = new TVDB(apiKey);
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Task[] bindingTasks = new Task[]
            {
            this.BindItemsSource<Mirrors, Mirror>(
                this.tvdbObj.RetrieveStaticData<Mirrors, Mirror>(), this.MirrorsListBox),

            this.BindItemsSource<Languages, Language>(
                this.tvdbObj.RetrieveStaticData<Languages, Language>(), this.LanguagesListBox)
            };

            await Task.WhenAll(bindingTasks);
        }

        private async Task BindItemsSource<T, U>(Task<T> retrievalTask, ListBox listboxToBindTo)
            where T : IXmlCollectionWrapper<U>
        {
            T retrievedData = await retrievalTask;
            listboxToBindTo.ItemsSource = retrievedData.InternalList;
        }
    }
}
